package cn.teaseed;

import cn.teaseed.OSC.oscin.VRCListener;
import cn.teaseed.comn.WebSocketServer;
import cn.teaseed.view.HomeView;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import javax.annotation.Resource;
import java.io.InputStream;
import java.net.SocketException;
import java.net.URL;
import java.util.Map;

@SpringBootApplication
public class Main extends Application {

    public static ConfigurableApplicationContext applicationContext;
    public static String[] args;

    @Resource
    private VRCListener vrcListener;

    @Override
    public void start(Stage stage) throws Exception {
        //窗口配置↓
        Scene scene = new Scene(loadView("home.fxml"));
        //加载标题
        stage.setTitle("VRChatOSCProject");
        //加载左上角图标
        InputStream resourceAsStream = getClass().getResourceAsStream("/png/logo.png");
        if (resourceAsStream != null) stage.getIcons().add(new Image(resourceAsStream));
        //固定大小
        stage.setResizable(false);
        //加载场景配置
        stage.setScene(scene);
        //显示窗口
        stage.show();
        //窗口居中
        stage.centerOnScreen();

        //为当前窗口添加关闭监听
        stage.setOnCloseRequest(event -> {
            WebSocketServer.closeAllWs();
        });
    }

    public static void main(String[] args) throws SocketException {
        Main.args = args;
        launch(args);
    }

    @Override
    public void init() {
        // 启动springboot
        applicationContext = SpringApplication.run(Main.class, args);
    }

    @Override
    public void stop() {
        // 关闭springboot
        applicationContext.stop();
    }

    public static Parent loadView(String fxmlName) throws Exception {
        // 加载 fxml
        if (fxmlName == null || fxmlName.isEmpty()) {
            throw new Exception();
        }
        URL resource = Main.class.getResource("/view/" + fxmlName);
        if (resource == null) {
            throw new Exception();
        }
        FXMLLoader loader = new FXMLLoader(resource);
        loader.setControllerFactory(applicationContext::getBean);
        return loader.load();
    }

    public static Stage newWindow(Scene scene, String fxmlName, Boolean closeWindow, Map<String, Object> data) throws Exception {
        Stage stage = new Stage();
        if (closeWindow) {
            stage = (Stage) scene.getWindow();
            stage.close();
        }
        //窗口配置↓
        Scene newScene = new Scene(loadView(fxmlName));
        //加载场景配置
        stage.setScene(newScene);
        //显示窗口
        stage.show();
        //窗口居中
        stage.centerOnScreen();

        stage.setUserData(data);

        InputStream resourceAsStream = Main.class.getResourceAsStream("/png/logo.png");
        if (resourceAsStream != null) stage.getIcons().add(new Image(resourceAsStream));
        return stage;
    }

    public static Stage newWindow(Scene scene, String fxmlName, Boolean closeWindow) throws Exception {
        return newWindow(scene, fxmlName, closeWindow, null);
    }
}